//
//  RowImageCell.m
//  
//  jhf
//
//  Created by 计海峰 on 2019/7/17.
//  Copyright © 2019 计海峰. All rights reserved.
//

#import "RowImageCell.h"
#import "ImageControlM.h"
#import <Masonry/Masonry.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import <CooperSDK/CooperSDK.h>
#import "RowImageBaseM.h"

@implementation RowImageCell

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    [self.bgV addSubview:self.detailIMG];
    [self.detailIMG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.bgV).insets(UIEdgeInsetsZero);
//        make.height.mas_equalTo(0).priorityLow();
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    @weakify(self);
    [_detailIMG addTapGestureRecognizer:^(UITapGestureRecognizer *recognizer, NSString *gestureId) {
        @strongify(self)
        BindModel *bind = (BindModel *)self.model;
        ImageControlM *info = (ImageControlM *)bind.model;
        if (info.imageUrls && info.isShow) {
            
        }
    }];
}

//MARK: - 点击事件
- (void)imageTap {
    BindModel *bind = (BindModel *)self.model;
    if ([bind.model isKindOfClass:RowImageBaseM.class]) {
        RowImageBaseM *info = (RowImageBaseM *)bind.model;
        if (info.method) {
            [bind.action run:info.method any:info];
        }else {
            NSLog(@"没有方法名");
        }
    }
}

- (void)bindTo:(__kindof id<BindProtocol>)model {
    [super bindTo:model];
    BindModel *bind = (BindModel *)model;
    ImageControlM *info = (ImageControlM *)bind.model;
    if ([bind.model isKindOfClass:RowImageBaseM.class]) {
        info = ((RowImageBaseM *)bind.model).imageControl;
    }
    if (info.isScale) {
        [self.detailIMG mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgV).offset(-info.paddingInsets.right);
        }];
    }else {
        [self.detailIMG mas_updateConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.bgV).insets(info.paddingInsets);
        }];
    }
    @weakify(self);
    [info controlIMG:self.detailIMG blockBool:^(BOOL result) {
        if (result) {
            @strongify(self);
            [self refreshCellHeight];
            if (info.isScale) {
                [self.detailIMG mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(self.bgV).offset(-info.paddingInsets.right);
                }];
            }
        }
//        else {
//            bind.cellHeight = 0.01;
//        }
    }];
}

- (void)refreshCellHeight {
    BindModel *bind = (BindModel *)self.model;
    ImageControlM *info = (ImageControlM *)bind.model;
    if ([bind.model isKindOfClass:RowImageBaseM.class]) {
        info = ((RowImageBaseM *)bind.model).imageControl;
    }
    if (bind.cellHeight != info.cellHeight) {
        bind.cellHeight = info.cellHeight;
        [bind.action runService:RefreshRowHeight any:bind.action.key];
    }
}


- (UIImageView *)detailIMG {
    if (!_detailIMG) {
        _detailIMG = [[UIImageView alloc] init];
        _detailIMG.userInteractionEnabled = true;
        @weakify(self);
        [_detailIMG addTapGestureRecognizer:^(UITapGestureRecognizer *recognizer, NSString *gestureId) {
            @strongify(self);
            [self imageTap];
        }];
    }
    return _detailIMG;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
